﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AvvaMobileNHibernateHelperKit
{
    public partial class CreateProjectDialog : Form
    {
        private MainForm ContainerForm;
        public CreateProjectDialog(MainForm container)
        {
            this.ContainerForm = container;
            InitializeComponent();
        }

        private void CreateProjectDialog_Load(object sender, EventArgs e)
        {
            treeProjectTypes.ExpandAll();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() == DialogResult.OK && fbd.SelectedPath != null)
            {
                txtProjectLocation.Text = fbd.SelectedPath;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            ProjectLanguages projectLanguage = ProjectLanguages.CSharp;
            switch (treeProjectTypes.SelectedNode.Tag.ToString())
            {
                case "Parent": Dialogs.ShowErrorBox("Please choose a project type.", "Create Project"); break;
                case "VisualBasic": projectLanguage = ProjectLanguages.VisualBasic; break;
                case "CSharp": projectLanguage = ProjectLanguages.CSharp; break;
            }

            Projects.CreateProject(txtProjectName.Text, txtProjectLocation.Text, projectLanguage);
            this.Close();
            AvvaMobileNHibernateHelperKit.Controls.Designer designerPage = new AvvaMobileNHibernateHelperKit.Controls.Designer();            
            designerPage.Dock = DockStyle.Fill;
            ContainerForm.pnlContent.Controls.Clear();
            ContainerForm.pnlContent.Controls.Add(designerPage);
        }

        private void TextChanged_(object sender, EventArgs e)
        {
            CheckDirtyFields();
        }

        private void CheckDirtyFields()
        {
            if (txtProjectName.Text == "" || txtProjectLocation.Text == "")
            {
                btnOk.Enabled = false;
            }
            else
            {
                switch (treeProjectTypes.SelectedNode.Tag.ToString())
                {
                    case "Parent": btnOk.Enabled = false; break;
                    default: btnOk.Enabled = true; break;
                }
            }


        }

        private void treeProjectTypes_AfterSelect(object sender, TreeViewEventArgs e)
        {
            CheckDirtyFields();
        }
    }
}